package utils

import (
	"errors"
	"log"
	"os"
	"os/exec"
)

/** 运行cmd */
func Command(cmd ...string) error {
	if len(cmd) == 0 {
		return errors.New("命令参数不能为空")
	}
	c := exec.Command(cmd[0], cmd[1:]...)
	c.Stdout = os.Stdout
	c.Stdin = os.Stdin
	c.Stderr = os.Stderr
	err := c.Start()
	if err != nil {
		return err
	}

	err = c.Wait()
	if err != nil {
		return err
	}

	return nil
}

/** 用shell 运行cmd **/
func Shell(cmd string) error {
	newCmd := []string{
		"/bin/sh",
		"-c",
		cmd,
	}
	log.Println("shell: " + cmd)
	return Command(newCmd...)
}
